# Copyright (c) 2011 Antoine d'Otreppe de Bouvette
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

from functools import wraps

class Flow(object):
    def __init__(self):
        self.avoid = False
        self.result = None
        self.exception = None
        self.args = None
        self.kwargs = None
        self.function = None

class Aspect(object):
    def atCall(self, flow):
        pass

    def atReturn(self, flow):
        pass

    def atRaise(self, flow):
        pass

    def __call__(self, func):
        @wraps(func)
        def newFunc(*args, **kwargs):
            flow = Flow()
            flow.args = args
            flow.kwargs = kwargs
            flow.function = func

            self.atCall(flow)
            if (flow.avoid):
                return flow.result

            try:
                flow.result = func(*flow.args, **flow.kwargs)
                self.atReturn(flow)
            except Exception as e:
                flow.exception = e
                if self.atRaise(flow):
                    raise e

            return flow.result

        return newFunc

class CompositeAspect(object):
    # One calldata, multiple aspects
    def __init__(self, *aspects):
        self.aspects = aspects

    def __call__(self, func):
        @wraps(func)
        def newFunc(*args, **kwargs):
            flow = Flow()
            flow.args = args
            flow.kwargs = kwargs
            flow.function = func

            for aspect in self.aspects:
                aspect.atCall(flow)
                if flow.avoid:
                    return flow.result

            try:
                flow.result = func(*flow.args, **flow.kwargs)
                for aspect in reversed(self.aspects):
                    aspect.atReturn(flow)
            except BaseException as e:
                flow.exception = e
                for aspect in reversed(self.aspects):
                    if aspect.atRaise(flow):
                        raise e

            return flow.result

        return newFunc

